﻿using Manage.Traceability.Api.DTO;
using Manage.Traceability.Domain;
using Manage.Traceability.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Manage.Traceability.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeepController : ControllerBase
    {
        private readonly ISqlSugarClient db;

        public DeepController(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加深加工信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddDeeps(DeepDTO dto)
        {
            DeepModel deeps = new DeepModel();
            deeps.DeepName = dto.DeepName;
            deeps.DeepUserName = dto.DeepUserName;
            int r1 = db.Insertable(deeps).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加加工信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加加工信息失败");
            }
        }
        /// <summary>
        /// 显示深加工信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<DeepDTO> ShowSlaughter()
        {
            var list = db.Queryable<DeepModel>()
               .Select(x => new DeepDTO
               {
                   DeepId=x.DeepId,
                   DeepName = x.DeepName,
                   DeepUserName = x.DeepUserName,
               }).OrderByDescending(x => x.DeepId).Take(1).First();
            return ApiResult<DeepDTO>.Success("显示养殖信息", list);
        }
    }
}
